<template>
    <el-container>
        <el-header style="height: 50px;border-bottom: none">
            <div class="left-panel">
                <div class="finish">
                    <div class="finishNum">总收入:<div class="text">{{ total_stock.total_income }}</div></div>
                    <div class="finishNum">总支出:<div class="text">{{ total_stock.total_expense }}</div></div>
                    <div class="finishNum">净收入:<div class="text">{{ total_stock.net_income }}</div></div>
                </div>
            </div>
        </el-header>
        <el-header>
            <div class="right-panel">
                <div class="right-panel-search">
                    <el-select v-model="search.account_id" placeholder="账户" filterable clearable>
                        <el-option v-for="item in accountList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                    <el-select v-model="search.inout_type" placeholder="收支类型" filterable clearable>
                        <el-option label="收入" value="1" />
                        <el-option label="支出" value="2" />
                    </el-select>
                    <el-select v-model="search.inout_id" placeholder="事项" filterable clearable>
                        <el-option v-for="item in matterList" :key="item.id" :label="item.name" :value="item.id" />
                    </el-select>
                    <el-select v-model="search.customer_id" placeholder="对方账户" filterable clearable>
                        <el-option v-for="item in customerList" :key="item.id" :label="item.customer_name" :value="item.id" />
                    </el-select>
                    <el-date-picker v-model="search.date" :value-format="'YYYY-MM-DD'" type="daterange" range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" />
                    <el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
                    <el-button type="primary" icon="el-icon-refresh" @click="refresh"></el-button>
                    <sc-import-button :auth="'record.import'" :apiObj="importObj" :base="base + '/file_templets/temples_record.xlsx'" @importSuccess="importSuccess" ></sc-import-button>
                    <sc-export-button :auth="'record.export'" :apiObj="apiObj" :search="search" :selection="selection"></sc-export-button>
                </div>
            </div>
        </el-header>
        <el-main class="nopadding">
            <scTable ref="table" :apiObj="apiObj" row-key="id" @selection-change="selectionChange" :params="search" border @setExtend="setExtend" tableName="recordTable" :column="recordColumn" @cell-click="cellClick">
                <el-table-column type="selection" width="50" align="center"></el-table-column>
                <template #inout_type="scope">
                    <el-tag :type="scope.row.inout_type == 1 ? 'success' : 'danger'">
                        {{scope.row.inout_type == 1 ? '收入' : '支出'}}
                    </el-tag>
                </template>
                <template #account="scope">
                    <span v-if="scope.row.account">{{`【${scope.row.account?.account_bank}】${scope.row.account?.account && /^\d+$/.test(scope.row.account.account) ? scope.row.account.account.slice(-4) : ''}`}}</span>
                    <span v-else>-</span>
                </template>
                <template #image="scope">
                    <el-image v-if="scope.row.image" style="width: 50px; height: 50px; border-radius: 5px;" :src="scope.row.image" :preview-src-list="[scope.row.image]" preview-teleported="true" hide-on-click-modal align="center"></el-image>
                </template>
                <!-- <el-table-column label="日期" prop="date" align="center"></el-table-column>
                <el-table-column label="事项" prop="inout.name" align="center"></el-table-column>
                <el-table-column label="类型" prop="inout_type" align="center">
                    <template #default="scope">
                        {{ scope.row.inout_type == 1 ? '收入' : '支出' }}
                    </template>
                </el-table-column>
                <el-table-column label="金额" prop="real_price" align="center"></el-table-column>
                <el-table-column label="账户" prop="account" align="center">
                    <template #default="scope">
                        <span v-if="scope.row.account">{{`【${scope.row.account?.account_bank}】${scope.row.account?.account && /^\d+$/.test(scope.row.account.account) ? scope.row.account.account.slice(-4) : ''}`}}</span>
                        <span v-else>-</span>
                    </template>
                </el-table-column>
                <el-table-column label="对方账户" prop="customer.customer_name" align="center"></el-table-column>
                <el-table-column label="图片" prop="image" align="center">
                    <template #default="scope">
                        <el-image v-if="scope.row.image" style="width: 50px; height: 50px; border-radius: 5px;" :src="scope.row.image" :preview-src-list="[scope.row.image]" preview-teleported="true" hide-on-click-modal align="center"></el-image>
                    </template>
                </el-table-column>
                <el-table-column label="备注" prop="remark" align="center"></el-table-column>
                <el-table-column label="创建人" prop="creator_name" align="center"></el-table-column> -->
            </scTable>
        </el-main>
        <select-house v-if="dialog.house" ref="houseDialog" @success="houseBack" @closed="dialog.house = false" :query="query"></select-house><!-- 入库记录 -->
    </el-container>
</template>

<script>
import SelectHouse from '@/views/business/selectHouse.vue';
import config from '@/config'
export default {
    name: '在库明细',
    components: {
        SelectHouse
    },
    data() {
        return {
            dialog: {
                house: false
            },
            apiObj: this.$API.business.finance.collect.record,
            base: config.API_URL,
			importObj: this.$API.business.finance.record.import,
            selection: [],
            search: {
                account_id: null,
                inout_type: null,
                inout_id: null,
                date: [this.moment().format('YYYY-MM-DD'), this.moment().format('YYYY-MM-DD')],
            },
            total_stock: {
                total_income: 0,
                total_expense: 0,
                net_income: 0,
            },
            accountList: [],
            matterList: [],
            recordColumn: [
                { label: "日期", prop: "date",},
                { label: "事项", prop: "inout.name",},
                { label: "类型", prop: "inout_type",},
                { label: "金额", prop: "real_price",},
                { label: "账户", prop: "account",},
                { label: "对方账户", prop: "customer.customer_name",},
                { label: "图片", prop: "image",},
                { label: "备注", prop: "remark",},
                { label: "创建人", prop: "creator_name",},
            ],
            customerList: [],
        }
    },
    mounted() {
        this.getAccount();
        this.getMatter();
        this.getCustomer();
    },
    methods: {
        cellClick(row, column){
            let is_search = false;
            // for (const key in this.search) {
            //     this.search[key] = null;
            // }
            if(column.property == 'inout.name'){
                this.search.inout_id = row.inout_id
                is_search = true;
            }else if(column.property == 'account'){
                this.search.account_id = row.account_id
                is_search = true;
            }else if(column.property == 'customer.customer_name'){
                this.search.customer_id = row.customer_id
                is_search = true;
            }
            if(is_search){ 
                this.upsearch();
            }
        },
        // 导入成功
        importSuccess(){
            this.$refs.table.refresh()
        },
        setExtend(data){
            this.total_stock = data
        },
        //表格选择后回调事件
        selectionChange(selection) {
            this.selection = selection;
        },
        upsearch(){
            this.$refs.table.upData(this.search)
        },
        refresh() {
            for (const key in this.search) {
                this.search[key] = null;
            }
            this.$refs.table.upData(this.search)
        },
		// 获取账户
        async getAccount() {
            var res = await this.$API.basic.account.all.get();
            const array = []
            res.data.map(item=>{
                array.push({
                    id: item.id,
                    name: `【${item.account_bank}】${/^\d+$/.test(item.account) ? item.account.slice(-4) : ''}`
                })
            })
            this.accountList = array
        },
        // 收支事项
        async getMatter() {
            var res = await this.$API.basic.matter.all.get();
            this.matterList = res.data
        },
        // 获取客户
        async getCustomer() {
            var res = await this.$API.basic.client.all.get();
            this.customerList = res.data
        },
    }
}
</script>

<style lang="scss" scoped>
table{
    text-align: center ;
}
</style>
